var Category = {
	initialize : function(settings) {
        layui.use(['form','element','layedit','table'], function(){
            var form = layui.form,
                element = layui.element,
                layedit = layui.layedit,
                table = layui.table;

            table.render({
                elem: '#category'
                ,even: true
                ,url:Base.url('page')
                ,cols: [[
                    {type:'checkbox', fixed: 'left'}
                    ,{field:'catid', title: 'ID', sort: true}
                    ,{field:'name',title: '名称'}
                    ,{field:'description', title: '描述'}
                    ,{field:'count',title: '文章总数', sort: true}
                    ,{title: '操作',align:'center', toolbar: '#barDemo'}
                ]]
                ,page: true
            });

            form.on('checkbox(category)', function(data){
                //是否被选中，true或者false
                var child = $('#category').find('tbody input[type="checkbox"][name="select"]');
                child.each(function(index, item){
                    item.checked = data.elem.checked;
                });
                form.render('checkbox');
            });

            //编辑和删除
            table.on('tool(category)', function(obj){
                var data = obj.data;
                if(obj.event === 'del'){
                    layer.confirm('你确定要删除分类吗？', function(index){
                        Category.delete_cate(data.catid);
                        layer.close(index);
                    });
                } else if(obj.event === 'edit'){
                    Category.edit_cate(data.catid, data.name, data.description);
                }
            });

            //操作
            var active = {
                add_category: function(){ //添加分类
                    add_category();
                }
                ,delete_category: function(){ //批量删除分类
                    var checkStatus = table.checkStatus('category'),
                        data = checkStatus.data;
                    Category.delete_cates(data);
                }
            };

            $('.operateTable .layui-btn').on('click', function(){
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });

            //添加日记
            var add_category = function(){
                layer.open({
                    type : 1,
                    offset:'150px',
                    area : '400px',
                    title : '添加分类',
                    btn:false,
                    content : $('#add_category_container').html(),
                    success : function(layero, index){
                        form.render();
                        form.verify({
                            name:[/(.+){2,}$/, '分类名称必须2位以上'],
                            description:[/(.+){2,}$/, '分类描述必须2位以上']
                        });

                        form.on('submit(add-category)', function(data){
                            layer.confirm('你确定要添加分类吗？',{
                                icon:3,
                                btn:['确定','取消'],
                                yes: function(index){
                                    var name = data.field['name'],
                                    	description = data.field['description'],
                                    	datas = {name: name,description: description};
                                    Base.ajax({
                                        url :Base.url('addCategory'),
                                        type:'post',
                                        data : {data: datas},
                                        success : function(data) {
                                            if (data.status == 'ok') {
                                                layer.msg(data.results,{time:1000},function(){
                                                    layer.closeAll();
                                                    Base.redirect(Base.url('category'));
                                                });
                                            } else {
                                                layer.msg(data.errdesc,{time:1500});
                                                return false;
                                            }
                                        }
                                    });
                                }
                            });              
                        });
                    }
                });     
            }
            form.render();
            
        });
    },

    //删除日记
    delete_cate : function(id){
        layer.load(3);
        Base.ajax({
            url :Base.url('deleteCategory'),
            type:'post',
            data : {id:id},
            success : function(data) {
                if (data.status == 'ok') {
                    layer.closeAll('loading');
                    layer.msg(data.results,{time:1000},function(){
                        layer.closeAll();
                        Base.redirect(Base.url('diary'));
                    });
                } else {
                    layer.msg(data.errdesc,{time:1500});
                    layer.closeAll('loading');
                    return false;
                }
            }
        });
    },
        
    //批量删除
    delete_cates : function(data){
        var ids = [];
        $.each(data, function(index,value){
            ids.push(value.catid);
        });

        if(ids.length == 0){
            layer.msg('请选择要删除的分类',{time:2000});
            return false;
        }

        layer.confirm('你确定要删除这些分类吗？',{
            icon:3,
            btn:['确定','取消'],
            yes: function(index){
                layer.load(3);
                Base.ajax({
                    url :Base.url('deleteCategory'),
                    type:'post',
                    data : {id:ids},
                    success : function(data) {
                        if (data.status == 'ok') {
                            layer.closeAll('loading');
                            layer.msg(data.results,{time:1000},function(){
                                layer.closeAll();
                                Base.redirect(Base.url('diary'));
                            });
                        } else {
                            layer.msg(data.errdesc,{time:1500});
                            layer.closeAll('loading');
                            return false;
                        }
                    }
                });
            }
        });
    },

    //编辑分类
    edit_cate : function(id, name, description){
        layer.open({
            type : 1,
            title : '编辑分类',
            btn:false,
            closeBtn:2,
            content : $('#edit_category_container').html(),
            success : function(layero, index){
                layui.use(['form','element','layedit'], function(){
                    var form = layui.form,
                        element = layui.element,
                        layedit = layui.layedit;

                    layero.find('input[name="name"]').val(name);
                    layero.find('input[name="description"]').val(description);

                    form.render();
                    form.verify({
                        name:[/(.+){2,}$/, '分类名称必须2位以上'],
                        description:[/(.+){2,}$/, '分类描述必须2位以上']
                    });


                    form.on('submit(edit-category)', function(data){
                        layer.confirm('你确定要更新分类吗？',{
                            icon:3,
                            btn:['确定','取消'],
                            closeBtn:2,
                            yes: function(index){

                                var name = data.field['name'],
                                    description = data.field['description'],
                                    datas = {catid: id,name: name,description: description};
                                Base.ajax({
                                    url :Base.url('editCategory'),
                                    type:'post',
                                    data : {data:datas},
                                    success : function(data) {
                                        if (data.status == 'ok') {
                                            layer.msg(data.results,{time:1000},function(){
                                                layer.closeAll();
                                                Base.redirect(Base.url('category'));
                                            });
                                        } else {
                                            layer.msg(data.errdesc,{time:1500});
                                            layer.closeAll('loading');
                                            return false;
                                        }
                                    }
                                });
                            }
                        });
                    });
                });
            }
        });
    }
}